<template>
  <header class="home-header">
    <div class="home-header-logo">
      <img src="../assets/logo.png"/>
    </div>
    <h1>后台管理系统</h1>
    <el-icon :size="30" color="#fff" class="fold" @click="handleCollapse">
      <component :is="isCollapse ? 'expand' : 'fold'"/>
    </el-icon>
    <div class="avatar">
      <el-avatar :size="32" src="./src/assets/logo.jpg"/>
      <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link">
            欢迎你，踹手手
          <el-icon class="el-icon--right"><arrow-down/></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item :icon="Edit" command="edit">修改密码</el-dropdown-item>
            <el-dropdown-item :icon="CircleClose" command="exit">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </header>
</template>

<script lang="ts">
import {defineComponent, ref} from 'vue'
import {
  Expand,
  Fold,
  Edit,
  CircleClose
} from '@element-plus/icons-vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import {useRouter} from 'vue-router'

export default defineComponent({
  name: "HomeHeader",
  components: {
    Expand,
    Fold,
  },
  props: {
    isCollapse: {
      type: Boolean,
      default: false
    }
  },
  emits: ['collapse'],
  setup(props, ctx) {
    const $router = useRouter()
    const handleCommand = (command: string | number | object) => {
      if (command === 'exit') {
        ElMessageBox.confirm(
            '确定要退出吗?',
            '提示',
            {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
            }
        ).then(() => {
          $router.push('/Login')
        }).catch(() => {
          ElMessage({
            type: 'info',
            message: '取消退出',
          })
        })
      }
    }
    const handleCollapse = () => {
      ctx.emit('collapse')
    }
    return {
      Edit,
      CircleClose,
      handleCollapse,
      handleCommand
    }
  }
})
</script>

<style lang="scss" scoped>
.home-header {
  display: flex;
  justify-content: start;
  align-items: center;
  height: 48px;
  background-color: #545c64;
  padding-left: 16px;

  .home-header-logo {
    width: 32px;
    height: 32px;
    margin-right: 8px;

    img {
      width: 100%;
      object-fit: cover;
    }
  }


  h1 {
    margin: 0;
    color: #fff;
    font-size: 24px;
  }

  .fold {
    cursor: pointer;
    margin-left: 20px;
    transition: all 1s;

    :hover {
      color: $myPrimaryColor;
    }
  }

  .avatar {
    position: absolute;
    display: flex;
    align-items: center;
    right: 30px;
  }

  span {
    color: #fff;
    margin-left: 10px;
    font-size: 14px;
  }
}

</style>